/*****************************************************************************/
/* File: Panel.cpp					                                         */
/* Author: rszulgo                                                           */
/*                                                                           */
/* Description:																 */
/* Class for the game panel.												 */
/*****************************************************************************/
#include "Panel.h"
#include "Picture.h"
#include <sstream>

//Constructor
CPanel::CPanel(CRenderer* pRenderer, HDC hDC, HWND hWnd)
{
	m_hDC = hDC;
	m_hWnd = hWnd;
	m_pRenderer = pRenderer;

	AddFontResource("data\\Fonts\\ABSTRACT.TTF");
	m_pFont = new CFont;
	CFont::SetDeviceContext(m_hDC);
	m_pFont->CreateFont("Abstract", 10, FW_NORMAL);

	// Get window size
	int width = GetSystemMetrics(SM_CXSCREEN);
	int height = GetSystemMetrics(SM_CYSCREEN);
	
	/* Create the text controls of the menu. */
	
	// player 1 score
	m_pPlayerOneScore = new CTextControl(m_pFont,TRectanglei(0,15,50,50));
	m_pPlayerOneScore->SetAlignement(CTextControl::TALeft);
	
	// player 1 lives
	m_pPlayerOneLife = new CTextControl(m_pFont,TRectanglei(0,15,width/3,width/3));
	m_pPlayerOneLife->SetAlignement(CTextControl::TALeft);

	// current level
	m_pLevel = new CTextControl(m_pFont,TRectanglei(0,15,width-50,width-50));
	m_pLevel->SetAlignement(CTextControl::TARight);

	// player 2 score
	m_pPlayerTwoScore = new CTextControl(m_pFont,TRectanglei(0,15,50,50));
	m_pPlayerTwoScore->SetAlignement(CTextControl::TALeft);
	
	// player 2 lives
	m_pPlayerTwoLife = new CTextControl(m_pFont,TRectanglei(0,15,width/3,width/3));
	m_pPlayerTwoLife->SetAlignement(CTextControl::TALeft);	
}

CPanel::~CPanel(void)
{
	if (m_pFont)
	{
		delete m_pFont;
		m_pFont = NULL;
	}

	RemoveFontResource("data\\Fonts\\ABSTRACT.TTF");
}

CPanel* CPanel::GetInstance(CRenderer* pRenderer, HDC hDC, HWND hWnd)
{
	static CPanel Instance(pRenderer, hDC, hWnd);
	return &Instance;
}

int CPanel::Update()
{
	stringstream ssText;

	ssText << "Punkty: " << m_liPlayerOneScore;
	m_pPlayerOneScore->SetText(ssText.str());

	ssText.str("");
	
	ssText << "^ycie: ";
	m_pPlayerOneLife->SetText(ssText.str());

	ssText.str("");

	ssText << "Poziom: " << m_iCurrentLevel;
	m_pLevel->SetText(ssText.str());

	ssText.str("");

	if (g_bTwoPlayers)
	{
		ssText << "Punkty: " << m_liPlayerTwoScore;
		m_pPlayerTwoScore->SetText(ssText.str());

		ssText.str("");
		
		ssText << "^ycie: ";
		m_pPlayerTwoLife->SetText(ssText.str());
	}

#ifdef SHOWFPS
	ssText.str("");

	ssText << "FPS: " << m_fFPS;
	m_pPlayerOneScore->SetText(ssText.str());

#endif
	return 0;
}

void CPanel::DrawBoat(int iX, int iY)
{
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	// Enable blending
	glEnable(GL_BLEND);
	glColor3f(1.0, 1.0, 1.0);
	TPicturePtr m_pBoatPicture = CPicture::CreatePicture("data\\images\\boat.png",TRectanglei(0,15,0,45));
	m_pBoatPicture->BlitPicture(iX,iY);
	glDisable(GL_BLEND);
}

int CPanel::Draw()
{
	int ile = 30;
	int width = GetSystemMetrics(SM_CXSCREEN);
	int height = GetSystemMetrics(SM_CYSCREEN);
	
	glEnable(GL_DEPTH_TEST);
	glDisable(GL_LIGHTING);
	glEnable(GL_LIGHT0);

	// Sets the size of the OpenGL viewport
    glViewport(0,0,width,height-height/ile);

	// Select the projection stack and apply 
	// an orthographic projection
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0,width,height-height/ile,0.0,-1.0,1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	m_pPlayerOneLife->Draw();

	for (int i=0; i<m_iPlayerOneLife; i++)
		DrawBoat(GetSystemMetrics(SM_CXSCREEN)/3+120 + i*45,0);

	m_pPlayerOneScore->Draw();
	m_pLevel->Draw();

	if (g_bTwoPlayers) 
	{
		// Sets the size of the OpenGL viewport
		glViewport(0,0,width,height/2-height/ile);

		// Select the projection stack and apply 
		// an orthographic projection
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0,width,height/2-height/ile,0.0,-1.0,1.0);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		
		m_pPlayerTwoLife->Draw();
		
		for (int i=0; i<m_iPlayerTwoLife; i++)
			DrawBoat(GetSystemMetrics(SM_CXSCREEN)/3+120 + i*45,0);

		m_pPlayerTwoScore->Draw();
		m_pLevel->Draw();
	}

	return 0;
}
